const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatTime3 = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  // const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
function checkPhone(phone) {
  if (!phone || !(/^1[345678]\d{9}$/.test(phone))) {
    return false;
  }
  return true;
}
function formatTime1(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}
function formatTime2(date) {
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [hour, minute, second].map(formatNumber).join(':')
}
function timestampToTime(timestamp) {

  var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = date.getDate();
  if (D < 10) {
    D = "0" + D
  }
  // var h = date.getHours() + ':';
  // var m = date.getMinutes() + ':';
  // var s = date.getSeconds();
  return Y + M + D;
}
function timestampToTimeMD(timestamp) {

  var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = date.getDate();
  if (D < 10) {
    D = "0" + D
  }
  // var h = date.getHours() + ':';
  // var m = date.getMinutes() + ':';
  // var s = date.getSeconds();
  return M + D;
}
function timestampToTimeYMDHM(timestamp) {

  var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = date.getDate();
  if (D < 10) {
    D = "0" + D
  }
  var h = date.getHours() + ':';
  var m = date.getMinutes() + ':';
  // var s = date.getSeconds();
  return Y + M + D + ' ' + h + m;
}

// 加载下一个页面 [Created by 2018-08-24 13:58 fxLDJ]
function navigate(url) {
  wx.navigateTo({
    url: url,
  })
}
// 销毁当前页面加载下一个页面 [Created by 2018-08-24 13:57 fxLDJ]
function redirect(url) {
  wx.redirectTo({
    url: url,
  });
}
// 上级页面对象 [Created by 2018-08-24 15:02 fxLDJ]
function prevPage(prevNum) {
  if (null == prevNum) {
    prevNum = 2;
  } else {
    prevNum++;
  }
  var pages = getCurrentPages();
  return pages[pages.length - prevNum];
}

/**
 *  页面头设置 [Created by 2018-09-12 09:54 fxLDJ]
 * @param title
 */
function pageTitle(title) {
  wx.setNavigationBarTitle({
    title: title
  });
}

// 提示框 [Created by 2018-07-03 08:36 fxLDJ]
function fxAlert(msg, callback) {
  if (null == callback) {
    setTimeout(function () {
      wx.showToast({
        duration: 2000,
        icon: "none",
        title: msg
      });
    }, 0);
  } else {
    wx.showModal({
      title: '温馨提示',
      content: msg,
      showCancel: false,
      success: function (res) {
        callback();
      }
    })
  }
}

function resetPhotoThumbUrl(model) {
  if (model.photoThumbUrl == undefined && model.photoUrl == undefined) {
    model.photoThumbUrl = ''
  } else if (model.photoThumbUrl == undefined && model.photoUrl != undefined) {
    model.photoThumbUrl = model.photoUrl;
  }
  return model;
}

function returnNN(varstr, defaultVar) {
  if (varstr == 0 && varstr != '' || varstr != undefined || varstr != null) {
    return varstr;
  }
  if (varstr == '' || varstr == undefined || varstr == null) {
    if (defaultVar != '' && defaultVar != undefined) {
      return defaultVar;
    }
    return '';
  }
  return varstr;
}

// 确认框 [Created by 2018-07-03 08:36 fxLDJ]
function fxConfirm(msg, params, cancelCallback) {
  var defaultParams = {
    title: '温馨提示',
    okValue: '确定',
    cancelValue: '取消',
    ok: function () { },
    cancel: function () { }
  };
  if (typeof params == "function") {
    defaultParams.ok = params;
    if (null != cancelCallback && typeof cancelCallback == "function") {
      defaultParams.cancel = cancelCallback;
    }
  } else {
    for (var key in params) {
      var one = params[key];
      if (null != one) {
        defaultParams[key] = one;
      }
    }
  }
  wx.showModal({
    title: defaultParams.title,
    content: msg,
    success: function (res) {
      if (res.confirm && null != defaultParams.ok) {
        defaultParams.ok();
      } else if (null != defaultParams.cancel) {
        defaultParams.cancel();
      }
    }
  })
}

// 设置默认图片 [Created by 2018-06-30 10:39 fxLDJ]
function defaultImg(model, key, defaultImg) {
  if (null == model[key] || model[key].trim() == "") {
    model[key] = defaultImg;
  }
  // return model ;
}
function navigateTo(url) {
  if (getCurrentPages().length >= 10) {
    wx.redirectTo({
      url: url
    })
  } else {
    wx.navigateTo({
      url: url,
    })
  }
}
module.exports = {
  resetPhotoThumbUrl: resetPhotoThumbUrl,
  formatTime: formatTime,
  checkPhone: checkPhone,
  formatTime1: formatTime1,
  timestampToTime: timestampToTime,
  timestampToTimeMD: timestampToTimeMD,
  timestampToTimeYMDHM: timestampToTimeYMDHM,
  formatTime2: formatTime2,
  formatTime3: formatTime3,
  navigate: navigate,
  redirect: redirect,
  prevPage: prevPage,
  pageTitle: pageTitle,
  fxAlert: fxAlert,
  returnNN: returnNN,
  fxConfirm: fxConfirm,
  navigateTo: navigateTo,
  defaultImg: defaultImg,
}
